package com.question;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.SynchronousQueue;

public class QuestionHM2 {

    public  static  void  main(String []args)
    {
        final BlockingQueue queue=new ArrayBlockingQueue(5);
        //final SynchronousQueue queue=new SynchronousQueue();
        final Semaphore sm=new Semaphore(1);

        for (int j=0;j<10;j++)
        {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        sm.acquire();
                        //sm.acquire();
                        String data= (String) queue.take();
                        String output= Test.process(data);
                        System.out.println(output);
                        sm.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }).start();
        }

        for(int i=0;i<10;i++)
        {
            String data=i+"";
            try {
                queue.put(data);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //process(data);
        }

    }
}
class Test{
    static String  process(String data)
    {
        try {

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Thread.currentThread().getName()+"处理数据——"+ data+"——在时间"+System.currentTimeMillis();

    }
}
